package iso3.pt.action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import iso3.pt.dao.IncorrectPasswordException;
import iso3.pt.dao.PtDAO;
import iso3.pt.dao.UserNotFoundException;
import iso3.pt.model.Alumno;
import iso3.pt.model.Asignatura;
import iso3.pt.model.Profesor;

import java.util.Date;
import java.util.Map;
import java.util.Set;




/**
 * <p> Validate a user login. </p>
 */
public class LoginAction  extends ActionSupport {


    public String execute() throws Exception {
        System.out.println("Validating login");
		if (getUsername() == null || getPassword() == null) {
			addActionError("Compulsory to specify both username and password!");
			return INPUT;
		}
		Map session = ActionContext.getContext().getSession();
		PtDAO ptdao = PtDAO.getInstance();
		Profesor prof;
		Alumno alumno;
		System.out.println(getRole());
		if (getRole().equals("2"))
		{
			try
			{
				if ((alumno = ptdao.loginAlumno(Integer.parseInt(getUsername()), getPassword())) != null )
				{
					setAlumno(alumno);
					session.put("alumno", alumno);
					subjects = ptdao.getAsignaturas(Integer.parseInt(getUsername()));
					return "listStudentSubjects";
				}
				else
					return ERROR;
			}
			catch (UserNotFoundException e) {
				addActionError("User " + getUsername() + " not found");
	            return ERROR;
			}
			catch (IncorrectPasswordException e) {
				addActionError("Incorrect password");
	            return ERROR;
			} 
			catch (Exception e)
			{
				return ERROR;
			}
		}
		else
		{
			try
			{
				if ((prof = ptdao.loginProfesor(Integer.parseInt(getUsername()), getPassword())) != null )
				{
					setProfesor(prof);
					session.put("profesor", prof);
					subjects = ptdao.getAsignaturasProfesor(profesor.getId());
					return "listLecturerSubjects";
				}
				else
					return ERROR;
			}
			catch (UserNotFoundException e) 
			{
				addActionError("User " + getUsername() + " not found");
				return ERROR;
			}
			catch (IncorrectPasswordException e) 
			{
				addActionError("Incorrect password");
        return ERROR;
			} 
			catch (Exception e)
			{
				return ERROR;
			}
		}
	}


    
    // ---- Username property ----

    /**
     * <p>Field to store User username.</p>
     * <p/>
     */
    private String username = null;


    /**
     * <p>Provide User username.</p>
     *
     * @return Returns the User username.
     */
    public String getUsername() {
        return username;
    }

    /**
     * <p>Store new User username</p>
     *
     * @param value The username to set.
     */
    public void setUsername(String value) {
        username = value;
    }

    // ---- Username property ----

    /**
     * <p>Field to store User password.</p>
     * <p/>
     */
    private String password = null;


    /**
     * <p>Provide User password.</p>
     *
     * @return Returns the User password.
     */
    public String getPassword() {
        return password;
    }

    /**
     * <p>Store new User password</p>
     *
     * @param value The password to set.
     */
    public void setPassword(String value) {
        password = value;
    }
    
    private String role = null;
    
    public String getRole()
    {
    	return role;
    }
    
    public void setRole(String value)
    {
    	role = value;
    }
    
    private Set<Asignatura> subjects;
    

    public Set<Asignatura> getSubjects()
    {
    	return subjects;
    }
    
    private Profesor profesor;
    
    public void setProfesor(Profesor profesor)
    {
    	this.profesor = profesor;
    }
    
    public Profesor getProfesor()
    {
    	return profesor;
    }
    
    private Alumno alumno;
    
    public void setAlumno(Alumno alumno)
    {
    	this.alumno = alumno;
    }
    
    public Alumno getAlumno()
    {
    	return alumno;
    }

}
